package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AccountService;
import com.qdlc.p2p.biz.service.AdditionalFlowUploadService;
import com.qdlc.p2p.biz.service.FinanFlowQueryService;
import com.qdlc.p2p.biz.service.UserBaseInfoService;
import com.qdlc.p2p.biz.service.UserCacheService;
import com.qdlc.p2p.biz.service.UserCertificationApplyService;
import com.qdlc.p2p.biz.service.UserService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.FinanFlowExecption;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Account;
import com.qdlc.p2p.dal.dto.AdditionalFlowProject;
import com.qdlc.p2p.dal.dto.AdditionalFlowUpload;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserBaseInfo;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.model.FlowInvestorModel;
import com.qdlc.p2p.dal.model.FlowProjectModel;
import com.qdlc.p2p.dal.model.UserBaseInfoModel;
import com.qdlc.p2p.dal.model.UserCacheModel;
import com.qdlc.p2p.dal.model.UserCertificationApplyModel;
import com.qdlc.p2p.dal.util.UserUtils;

public class FlowAction extends BaseAction<FlowProjectModel> implements
ModelDriven<FlowProjectModel>{
	
	@Resource
	private FinanFlowQueryService finanFlowQueryService;
	
	
	@Resource
	private AccountService accountService;
	
	@Resource
	private UserCacheService userCacheService;
	@Resource
	private UserBaseInfoService userBaseInfoService;
	@Resource
	private UserCertificationApplyService userCertificationApplyService;
	@Resource
	private UserService userService;
	@Resource
	private AdditionalFlowUploadService additionalFlowUploadService;
	
	
	private Map<String, Object> data;
	
	@Action("/findFlowIndexJson")
	public void findFlowIndexJson() throws IOException{
		List<FlowProjectModel> list = finanFlowQueryService.getFlowProjectIndex();
		data = new HashMap<String, Object>();
		PageDataList<FlowProjectModel> pagelist = new PageDataList<FlowProjectModel>();
		pagelist.setList(list);
		data.put("data", pagelist);
		printWebJson(getStringOfJpaObj(data));
	}

	
	@Action(value="/flow/detailFlow",interceptorRefs = {
			@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public String detailFlow(){
		String uuid = paramString("uuid");
		AdditionalFlowProject project = finanFlowQueryService.getProjcetByUuid(uuid);
		if (null == project) {
			throw new FinanFlowExecption("查看项目异常！");
		}
		request.setAttribute("uuid", project.getUuid());
		request.setAttribute("bid",project.getId());
		return "detailflow";
	}
	
	@Action("/flow/detailFlowJson")
	public void detailFlowJson() throws IOException{
		saveToken("flowtenderToken");
		String flowtenderToken = (String) session.get("flowtenderToken");
		String uuid = paramString("uuid");
		AdditionalFlowProject project = finanFlowQueryService.getProjcetByUuid(uuid);
		data = new HashMap<String, Object>();
		FlowProjectModel flowModel = FlowProjectModel.instance(project);
		flowModel.setUser(null);
		flowModel.setOperator(null);
		data.put("project", flowModel);
		User user = userService.getUserById(this.getSessionUser().getUserId());
		Account account = accountService.findAccountByUserId(user.getUserId());
		data.put("useMoney", account.getUseMoney());
		UserCache userCache = userCacheService.findByUserId(project.getUser().getUserId());
		UserCacheModel uc = UserCacheModel.instance(userCache);
		String companyName = uc.getCompanyName();
		if (companyName != null && companyName.length() > 5) {
			uc.setCompanyName(companyName.substring(0, 2) + "****" + companyName.substring(companyName.length() - 4));
		}
		data.put("uc", uc);
		UserBaseInfo userBaseInfo = userBaseInfoService.findByUserId(project.getUser().getUserId());
		if (userBaseInfo != null) {
			UserBaseInfoModel userBaseInfoModel = UserBaseInfoModel.instance(userBaseInfo);
			String username = project.getUser().getUserName();
			String hideName = username.charAt(0) + "******" + username.charAt(username.length() - 1);
			userBaseInfoModel.setUserName(hideName);
			userBaseInfoModel.setUser(null); // 防止前台查看用户信息
			data.put("userBaseInfo", userBaseInfoModel);
		}
		List<UserCertificationApplyModel> certificationApply = userCertificationApplyService.findByUser(project.getUser());
		data.put("certificationApply", certificationApply);
		PageDataList<FlowInvestorModel> list = finanFlowQueryService.getInvestorList(project.getId(), model.getPage(), model.getRows());
		data.put("data", list);
		data.put("flowtenderToken", flowtenderToken);
		UserCache cache = UserUtils.getUserCacheByUserId(user);
		data.put("userType", cache.getUserType());
		//判断是否已经设置支付密码
		if (StringUtil.isBlank(user.getPayPwd())) {
			data.put("isPay", false);
		}else{
			data.put("isPay", true);
		}
		//流转理财图片
		List<AdditionalFlowUpload> flowUpload = additionalFlowUploadService.findByFlowProjectId(project.getId());
		List<AdditionalFlowUpload> flowUploads = new ArrayList<AdditionalFlowUpload>();
		for(AdditionalFlowUpload u : flowUpload){
			AdditionalFlowUpload fu = new AdditionalFlowUpload();
			fu.setType(u.getType());
			fu.setPicPath(u.getPicPath());
			flowUploads.add(fu);
		}
		data.put("flowUploads", flowUploads);
		data.put("url", SystemConfigHelper.getValue(Nid.IMAGE_SERVER_URL));
		
		printWebJson(getStringOfJpaObj(data));
		
	}
	
	

	/**
	 * 理财项目我要投资列表
	 * @return
	 */
	@Action(value = "/flow/flowIndex", results = { @Result(name = "flow", type = "ftl", location = "/invest/flow.html") })
	public String flowIndex(){
		return "flow";
		
	}
	
	/**
	 * 理财项目异步获取数据
	 * @throws IOException
	 */
	@Action("/flow/flowIndexJson")
	public void flowIndexJson() throws IOException{
		data = new HashMap<String, Object>();
		
		PageDataList<FlowProjectModel> pageList = finanFlowQueryService.getindexFlowList(model);
		data.put("data", pageList);
		printWebJson(getStringOfJpaObj(data));
		
		
	}
	
	
	
}
